import elementtree.ElementTree as ET
import SyntaxParser
import Result
import repository.hofunctions as hof

class result_parser:
    def __init__(self,inputfilename, outputfilename, events, results, notes):
        self.InputFile = inputfilename
        self.OutputFile = outputfilename
        self.events = events
        self.Results = results
        self.Notes = notes                           
        self.assign_value()
                
    def assign_value(self):
        root = ET.parse(self.InputFile)
        iter = root.getiterator()
        matchresult = []
        for element in iter:
            if element.tag == "Textinput":
                if element.text is not None:
                    keyerror = True
                    staterror = False
                    for valueelement in element.getchildren():
                        if valueelement.tag == "Value":                            
                            if len(valueelement.getchildren()) > 0:
                                print "good"
                                for statisticelement in valueelement.getchildren():
                                   if statisticelement.tag == "Statistic":                                                                                                                                                                                                                            
                                      for result in self.Results:
                                         matchresult = hof.match(result,self.events, element.text, statisticelement.text)
                                         print matchresult
                                         if matchresult == "StatError":
                                            staterror = True
                                            keyerror = False
                                         if matchresult != "KeyError" and matchresult != "StatError":
                                            staterror = False
                                            keyerror = False
                                            break
                                      if len(matchresult) != 0:
                                          valueelement.text = str(matchresult[0])
                                      else:
                                          valueelement.text = "None"
                            
                                      if keyerror:
                                          valueelement.text = "Not Applicable"
                                      if staterror:
                                          valueelement.text = "Stat Error"                                   
                            else:                                
                                if len(self.Results) > 0:
                                    print "bad"
                                    for result in self.Results:
                                        matchresult = hof.match(result,self.events,element.text)
                                        print matchresult
                                        if matchresult == "StatError":
                                            staterror = True
                                            keyerror = False
                                        if matchresult != "KeyError" and matchresult != "StatError":
                                            staterror = False
                                            keyerror = False
                                            break

                                    if len(matchresult) != 0:
                                        valueelement.text = str(matchresult[0])
                                    else:
                                        valueelement.text = "None"
                                    
                                    if keyerror:
                                        valueelement.text = "Not Applicable"
                                    if staterror:
                                        valueelement.text = "Stat Error"
                                else:
                                    valueelement.text = "Not Applicable"

#                                    
#                            for statisticelement in valueelement.getchildren():
#                                if statisticelement.tag == "Statistic":
#                                    if statisticelement.text is not None:
#                                        index = index + 1
#                                    else:
#                                        index = index + 1
#                            if len(valueelement.getchildren()) == 0:
#                                    index = index + 1
            if element.tag == "Textarea":
                if element.text is not None:
                    for valueelement in element.getchildren():
                        if valueelement.tag == "Value":
                           values = self.Notes.parse(str(element.text).strip().lower())
                           if values is not None:
                               for value in values:
                                   if valueelement.text is not None:
                                       valueelement.text = valueelement.text + str(value) + "\n"
                                   else:
                                       valueelement.text = str(value) + "\n"
                                       
                                      
        root.write(self.OutputFile)

    def elementCheck(self,element):
      if element.tag == "Textinput":
           self.isTextinput(element)
      if element.tag == "Textarea":
           self.isTextarea(element)

    def isTextinput(self, element):
      for child in element.getchildren():
         if child.tag == "Value":
            child.text = "Result"
      
    def isTextarea(self, element):
      for child in element.getchildren():
         if child.tag == "Value":
            child.text = "Result"
       

